package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.epfresh.api.dialog.TimePickerDialog;
import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.AppActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.widget.CommonInputItem;
import com.epfresh.supplier.R;
import com.google.gson.Gson;

import java.util.Calendar;

/**
 * Created by wzh on 16-7-25.
 */
public class ProductOtherActivity extends AppActivity {


    private CommonInputItem ciBrand;
    private CommonInputItem ciArea;
    private CommonInputItem ciPickTime;

    String area;
    String brand;
    String time;
    String type;
    private String productId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_product_other);


        if (savedInstanceState != null) {
            getIntent().putExtra("productId", savedInstanceState.getString("productId"));
            getIntent().putExtra("area", savedInstanceState.getString("area"));
            getIntent().putExtra("brand", savedInstanceState.getString("brand"));
            getIntent().putExtra("time", savedInstanceState.getString("time"));
            getIntent().putExtra("type", savedInstanceState.getString("type"));
        }
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("其他");

        rightbtn.setText("保存");
        rightbtn.setVisibility(View.VISIBLE);

        ciBrand = (CommonInputItem) findViewById(R.id.ci_brand);
        ciArea = (CommonInputItem) findViewById(R.id.ci_area);
        ciPickTime = (CommonInputItem) findViewById(R.id.ci_pick_time);

        ciPickTime.setOnClickListener(this);


        area = getIntent().getStringExtra("area");
        brand = getIntent().getStringExtra("brand");
        time = getIntent().getStringExtra("time");
        type = getIntent().getStringExtra("type");
        productId = getIntent().getStringExtra("productId");

        ciBrand.setContentText(brand);
        ciBrand.setSelectionLast();
        ciArea.setContentText(area);
        ciPickTime.setContentText(time);


        if ("1".equals(type)) {//现货商品
            ciPickTime.setVisibility(View.VISIBLE);
        } else if ("2".equals(type)) {//预售商品
            ciPickTime.setVisibility(View.GONE);
        }

    }

    @Override
    public void onRightClick() {
        save();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.ci_pick_time:
                openPickerTime(ciPickTime.getContentText());
                break;


        }
    }

    private void save() {

        String area = ciArea.getContentText();
        String brand = ciBrand.getContentText();
        String time = ciPickTime.getContentText();

        if ("".equals(area) || "".equals(brand)){
            return;
        }
        if ("1".equals(type) && "".equals(time)){
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("area", area);
        intent.putExtra("brand", brand);
        intent.putExtra("time", time);

        setResult(RESULT_OK, intent);

        if (productId != null && !"".equals(productId)) {


            if ("1".equals(type)) {//现货商品

            } else if ("2".equals(type)) {//预售商品
                time = null;
            }

            requestProductModify(area, brand, time);

        } else
            finish();
    }


    private void openPickerTime(String time) {

        TimePickerDialog timeDialog = new TimePickerDialog(this);
        timeDialog.setTitle(getString(R.string.pick_time));
        timeDialog.setOnPositiveClickListener(new TimePickerDialog.OnPositiveClickListener() {
            @Override
            public void onPositiveClickListener(int index, String data) {
                ciPickTime.setContentText(data);
            }
        });

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        int ey = calendar.get(Calendar.YEAR);
        int em = calendar.get(Calendar.MONTH) + 1;
        int ed = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.YEAR, -1);
        int sy = calendar.get(Calendar.YEAR);
        int sm = calendar.get(Calendar.MONTH) + 1;
        int sd = calendar.get(Calendar.DAY_OF_MONTH);
        timeDialog.setStartYear(sy);
        timeDialog.setStartMonth(sm);
        timeDialog.setStartDay(sd);

        timeDialog.setEndYear(ey);
        timeDialog.setEndMonth(em);
        timeDialog.setEndDay(ed);
        timeDialog.setCurrentDate(time);

        timeDialog.show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x92 && resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");
            ciBrand.setContentText(result);
        } else if (requestCode == 0x93 && resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");
            ciArea.setContentText(result);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putString("area", area);
        outState.putString("brand", brand);
        outState.putString("time", time);
        outState.putString("type", type);
        outState.putString("productId", productId);
        super.onSaveInstanceState(outState);
    }

    private void requestProductModify(String origin, String brand, String pluckingTime) {

        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        RequestEntityMapSupply.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_modify);

        if (origin != null && !"".equals(origin))
            RequestEntityMapSupply.putParameter("origin", origin);
        if (brand != null && !"".equals(brand))
            RequestEntityMapSupply.putParameter("brand", brand);
        if (pluckingTime != null && !"".equals(pluckingTime))
            RequestEntityMapSupply.putParameter("pluckingTime", pluckingTime);
        RequestEntityMapSupply.putParameter("id", productId);
        request(RequestEntityMapSupply, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_product_modify, onProductModifyRequestListener);

    }

    private OnRequestListener<StatusEntity> onProductModifyRequestListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {

            hideProgressDialog();
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {

                finish();
            } else {
                //没有数据
                toast("修改失败");

            }

        }
    };
}
